package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.WorldCountries;
import cn.s1995.sloth.bizs.service.WorldCountriesService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 国家
 *
 * @author Lex
 * @date 2024-01-18 10:21:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/worldCountries")
@Tag(description = "worldCountries", name = "BMS-国家管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class WorldCountriesController {

    private final WorldCountriesService worldCountriesService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 国家
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject WorldCountries req) {
        return R.ok(worldCountriesService.page(page, buildQueryWrapper(req)));
    }

    /**
     * 获取列表
     * @param req 国家
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_view')")
    public R getList(@ParameterObject WorldCountries req) {
        return R.ok(worldCountriesService.list(buildQueryWrapper(req)));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_view')")
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(worldCountriesService.getById(id));
    }

    /**
     * 新增国家
     * @param req 国家
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增国家", description = "新增国家")
    @SysLog("新增国家")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_add')")
    public R save(@RequestBody WorldCountries req) {
        return R.ok(worldCountriesService.save(req));
    }

    /**
     * 修改国家
     * @param req 国家
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改国家", description = "修改国家")
    @SysLog("修改国家")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_edit')")
    public R updateById(@RequestBody WorldCountries req) {
        return R.ok(worldCountriesService.updateById(req));
    }

    /**
     * 通过id删除国家
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除国家", description = "通过id删除国家")
    @SysLog("通过id删除国家")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_del')")
    public R remove(@RequestBody Integer[] ids) {
        return R.ok(worldCountriesService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_worldCountries_export')")
    public List<WorldCountries> export(WorldCountries req, Integer[] ids) {
        return worldCountriesService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), WorldCountries::getId, ids));
    }

    /**
     * 构建查询条件
     * @param req
     * @return
     */
    @NotNull
    private static LambdaQueryWrapper<WorldCountries> buildQueryWrapper(WorldCountries req) {
        LambdaQueryWrapper<WorldCountries> wrapper = Wrappers.lambdaQuery(WorldCountries.class)
                .and(
                        StrUtil.isNotEmpty(req.getSearch()),
                        i -> i.like(WorldCountries::getName, req.getSearch())
                                .or()
                                .like(WorldCountries::getTranslations, req.getSearch())
                                .or()
                                .like(WorldCountries::getIso2, req.getSearch())
                                .or()
                                .like(WorldCountries::getIso3, req.getSearch())
                                .or()
                                .like(WorldCountries::getNumericCode, req.getSearch())
                                .or()
                                .like(WorldCountries::getPhonecode, req.getSearch())
                                .or()
                                .like(WorldCountries::getCapital, req.getSearch())
                                .or()
                                .like(WorldCountries::getCurrency, req.getSearch())
                                .or()
                                .like(WorldCountries::getCurrencyName, req.getSearch())
                                .or()
                                .like(WorldCountries::getCurrencySymbol, req.getSearch())
                                .or()
                                .like(WorldCountries::getTld, req.getSearch())
                                .or()
                                .like(WorldCountries::getNatives, req.getSearch())
                                .or()
                                .like(WorldCountries::getRegion, req.getSearch())
                                .or()
                                .like(WorldCountries::getSubregion, req.getSearch())
                                .or()
                                .like(WorldCountries::getTimezones, req.getSearch())
                );
        return wrapper;
    }
}
